//
//  HTTPRequestHelper.m
//  eBook
//
//  Created by Pham Vu Duong 16/11/2011
//  Copyright 2011 CMCSoft. All rights reserved.
//

#import "TumblrRequestHelper.h"
//#import "Constants.h"

@implementation TumblrRequestHelper
@synthesize httpRequestMethod,notificationReturn;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)requestWithMethodName:(HTTPRequestMethod)methodName
                       andURL:(NSString*)URL
                andHttpMethod:(NSString *)httpMethod
                  andDelegate:(id<ASIHTTPRequestDelegate>)delegate
{
    [self setHttpRequestMethod:methodName];
    _request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:[URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    [_request setDelegate:delegate];
    [_request startAsynchronous];
}

- (void)dismissRequest
{
    // Cancels an asynchronous request
    [_request cancel];
    // Cancels an asynchronous request, clearing all delegates and blocks first
    [_request clearDelegatesAndCancel];
}

#pragma mark - LIST POSTS

- (NSString*)getListPostWithURL:(NSString*)url
{
    [self requestWithMethodName:REQUEST_POSTS_LIST andURL:url andHttpMethod:@"GET" andDelegate:self];
    
    notificationReturn = [[NSString alloc] init];
    notificationReturn = kRequestListPostsNotification;
    return kRequestListPostsNotification;
}





#pragma mark - HTTPRequestHelper Delegate

- (void)requestFinished:(ASIHTTPRequest *)request
{ 
    NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];
    
    if (self.httpRequestMethod == REQUEST_POSTS_LIST) 
    {
        [notificationCenter postNotificationName:kRequestListPostsNotification object:[request responseString] userInfo:nil];
    }

}

- (void)requestFailed:(ASIHTTPRequest *)request
{

    NSError *error = [request error];
    NSLog(@"%@",[error description]);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kRequestListFailedNotification" 
                                                        object:[NSString stringWithFormat:@"%d",self.httpRequestMethod]];
}

@end
